<template>
    <div class="welcome-page main-card">
        <section class="product-drawing" :style="{ height: `${Size.containerHeight}px` }">
            <img src="@/assets/images/wizard/productdrawing.png" alt="product drawing" draggable="false" :style="{ width: `${Size.productSize}px` }" />
        </section>

        <section class="submit-content">
            <div class="submit-card">
                <div class="welcome-text">
                    <p>{{ $t('wizard.welcomeMessage') }}</p>
                </div>
            </div>
        </section>

        <section class="btn-content">
            <el-button type="primary" @click="startUsing">{{ $t('wizard.startUsing') }}</el-button>
        </section>

        <!-- 《Terms and Conditions》 -->
        <confirmDialog ref="agreementRef" :confirmText="confirmText" :cancelText="cancelText" :title="title" :isShowFooter="isShowFooter" @confirm="confirm" @cancel="cancel" :isShowCountdown="false">
            <div style="max-height: 400px; overflow: auto" ref="agmContentRef">
                <p v-for="item in 588" :key="item">我是协议内容 -> {{ item }}</p>
            </div>
        </confirmDialog>
    </div>
</template>

<script>
import { debounce } from 'lodash-es';

import { WIZARD_ROUTE, setWizardHistory } from './wizard.data';

export default {
    name: 'Welcome',
    data() {
        return {
            conditions: true,
            isRead: false,
            isShowFooter: false,
            confirmText: this.$t('security.accessControl.accept'),
            cancelText: this.$t('security.accessControl.reject'),
            title: this.$t('wizard.termsAndConditions')
        };
    },
    methods: {
        // 检查是否已读
        checkRead() {
            if (!this.isRead) {
                this.conditions = false;
                this.showAgreementDialog();
            }
        },

        // 打开协议弹窗
        showAgreementDialog() {
            this.isShowFooter = false;
            this.conditions = false;
            this.$refs.agreementRef.show();

            // 增加弹窗滚动监听
            this.$nextTick(() => {
                const scrollableDiv = this.$refs.agmContentRef;
                if (scrollableDiv) {
                    scrollableDiv.addEventListener('scroll', this.checkScrollToBottom);
                }
            });
        },

        // 检查协议是否已经滚到到底部
        checkScrollToBottom() {
            const scrollableDiv = this.$refs.agmContentRef;
            if (scrollableDiv) {
                // 获取滚动元素的可见高度和滚动高度
                const visibleHeight = scrollableDiv.clientHeight;
                const scrollHeight = scrollableDiv.scrollHeight;

                // 计算元素的滚动位置
                const scrollTop = scrollableDiv.scrollTop;

                // 判断是否滚动到底部
                const isScrolledToBottom = scrollHeight - scrollTop - visibleHeight <= 1; // 添加一个允许的误差范围，因为存在小数计算误差
                if (isScrolledToBottom) if (!this.isShowFooter) this.isShowFooter = true;
            }
        },

        // 同意协议
        confirm() {
            this.conditions = true;
            this.$refs.agreementRef.hide();
            this.removeScrollWatch();
        },

        // 拒绝协议
        cancel() {
            this.isShowFooter = false;
            this.conditions = false;
            this.removeScrollWatch();
        },

        // 移除滚动监听
        removeScrollWatch() {
            this.$nextTick(() => {
                const scrollableDiv = this.$refs.agmContentRef;
                if (scrollableDiv) {
                    scrollableDiv.removeEventListener('scroll', this.checkScrollToBottom);
                }
            });
        },

        // 开始使用
        startUsing: debounce(async function () {
            // this.checkRead();
            this.$router.replace(WIZARD_ROUTE.ConnectionDetection);
            setWizardHistory(WIZARD_ROUTE.ConnectionDetection);
        }, 200)
    },
    computed: {
        Size() {
            const defaultSize = { productSize: 288, containerHeight: 280 };
            const projectSize = {};

            return { ...defaultSize, ...projectSize };
        }
    }
};
</script>

<style lang="less" scoped>
.welcome-page {
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    font-size: 24px;

    width: 100%;
    height: 100%;

    padding: 20px;
    box-sizing: border-box;

    .product-drawing,
    .welcome-text {
        user-select: none;
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        text-align: center;
    }

    .product-drawing {
        height: 280px;
        display: flex;
        justify-content: center;
        align-items: center;

        img {
            object-fit: cover;
        }
    }

    .submit-content {
        display: flex;
        .submit-card {
            margin: auto;
            width: 500px;

            // .welcome-text {
            //     height: 80px;
            // }

            .agreement,
            .note {
                height: 50px;
            }

            .agreement {
                display: flex;
                align-items: flex-start;
                flex-wrap: wrap;
                .conditions-box {
                    // min-width: 170px;
                    padding-right: 5px;
                }
            }

            @media (max-width: 390px) {
                .agreement {
                    height: 60px;
                    .tac-link {
                        margin-left: 20px;
                    }
                }
            }

            .note {
                line-height: 19px;
                font-size: 14px;
            }
        }
    }

    .btn-content {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 150px;

        /deep/.el-button {
            width: 450px;
        }
    }
}

/deep/.el-checkbox__input.is-checked + .el-checkbox__label {
    color: black;
}

/deep/.el-checkbox__label {
    display: inline-grid;
    max-width: 450px;
    white-space: pre-line;
    word-wrap: break-word;
    line-height: 20px;
}

/* 定义关键帧 */
@keyframes shake {
    0%,
    100% {
        transform: translateX(0);
    }
    10%,
    30%,
    50%,
    70%,
    90% {
        transform: translateX(-5px);
    }
    20%,
    40%,
    60%,
    80% {
        transform: translateX(5px);
    }
}
/* 应用动画效果 */
.shake {
    animation: shake 1s infinite;
}

// 斜体文字
.italic-text {
    font-style: italic;
}

/deep/.el-dialog {
    max-width: 1273px !important;
    width: 100%;
}
</style>
